<?php

/**
 * 首页
 * @copyright (c) 2013, Mr.Wang
 * @author Mr.Wang <281762391@qq.com>
 */
class AjaxAction extends HomeAction {

    /**
     * ajax 传递省、市、区参数
     */
    public function ajax_address() {
        $Result = D('Area')->address();
        $this->ajaxReturn($Result['data'], $Result['msg'], $Result['status']);
    }

    /**
     * 发送站内信 
     */
    public function sendMessage() {
        $uid     = session('uid');
        $message = t($_REQUEST['message']);
        $title   = t($_REQUEST['title']);
        $to_uid  = format_int($_REQUEST['tuid']);
        //用户是否登录
        if (false == $uid)
            $this->ajaxReturn(reStatus(false, '您尚未登录'));
        if (false == $title)
            $this->ajaxReturn(reStatus(false, '标题不能为空'));
        if (false == $message)
            $this->ajaxReturn(reStatus(false, '发送信息不能为空'));
        if (false == $to_uid)
            $this->ajaxReturn(reStatus(false, '发送对象不能为空'));
        $data    = array(
            'from_uid'    => $uid,
            'to_uid'      => $to_uid,
            'title'       => $title,
            'content'     => $message,
            'is_read'     => 0,
            'create_time' => time(),
        );
        $res          = M('Message')->add($data);
        if ($res) {
            $this->ajaxReturn(reStatus(true, '发送成功'));
        } else {
            $this->ajaxReturn(reStatus(false, '发送失败'));
        }
    }

    /**
     * 未读站内信 
     */
    public function countMessage() {
        $uid = session('uid');
        if ($uid == false) {
            echo 0;
            exit;
        }
        $map = array('to_uid'  => $uid, 'is_read' => 0);
        $count    = M('Message')->where($map)->count(0);
        echo intval($count);
        exit;
    }

    /**
     * 发表评论 
     */
    public function pushCommit() {
        $uid      = session('uid');
        $title    = t($_REQUEST['title']);
        $content  = t($_REQUEST['content']);
        $con_id   = format_int($_REQUEST['con_id']);
        $con_type = t($_REQUEST['con_type']);
        //用户是否登录
//        if (false == $uid)
//            $this->ajaxReturn(reStatus(false, '您尚未登录'));
        if (false == $title)
            $this->ajaxReturn(reStatus(false, '标题不能为空'));
        if (false == $content)
            $this->ajaxReturn(reStatus(false, '评论内容不能为空'));
        if (false == $con_id || !in_array($con_type, array('dazhe', 'service')))
            $this->ajaxReturn(reStatus(false, '参数错误'));
        $commit = array(
            'title'       => $title,
            'content'     => $content,
            'con_id'      => $con_id,
            'con_type'    => $con_type,
            'from_uid'    => $uid ? $uid : 0,
            'create_time' => time(),
        );
        $res          = M('Comment')->add($commit);
        $reData       = reStatus($res ? true : false, $res ? '评论成功' : '评论失败');
        $this->ajaxReturn($reData);
    }

    /**
     * 添加收藏 
     */
    public function addFavorite() {
        $uid      = session('uid');
        $title    = t($_REQUEST['title']);
        $url      = t($_REQUEST['jurl']);
        $con_id   = format_int($_REQUEST['con_id']);
        $con_type = $_REQUEST['con_type'];
        //用户是否登录
        if (false == $uid)
            $this->ajaxReturn(reStatus(false, '您尚未登录'));
        if (false == $con_id || !in_array($con_type, array('dazhe', 'service')))
            $this->ajaxReturn(reStatus(false, '参数错误'));
        //是否已收藏
        $favMod = M('UserFavorite');
        $count  = $favMod->where(array('con_id'   => $con_id, 'con_type' => $con_type, 'user_id'  => $uid))->count(0);
        if ($count > 0)
            $this->ajaxReturn(reStatus(false, '您已收藏过该'));
        if (!$title || $url) {
            switch ($con_type) {
                case 'dazhe':
                    $dazhe = M('Dazhe')->find($con_id);
                    $title = str_cut($dazhe['description'], 40, '...');
                    $url   = U('Home/Dazhe/show', array('id'     => $con_id));
                    break;
                case 'service':
                    $service = M('Service')->find($con_id);
                    $title   = str_cut($service['name'], 40, '...');
                    $url     = U('Home/Service/show', array('sid'     => $con_id));
                    break;
            }
        }
        if (false == $title)
            $this->ajaxReturn(reStatus(false, '标题不能为空'));
        if (false == $url)
            $this->ajaxReturn(reStatus(false, '链接不能为空'));
        $favorite = array(
            'title'       => $title,
            'url'         => $url,
            'con_id'      => $con_id,
            'con_type'    => $con_type,
            'user_id'     => $uid,
            'create_time' => time(),
        );
        $res          = $favMod->add($favorite);
        $reData       = reStatus($res ? true : false, $res ? '收藏成功' : '收藏失败');
        $this->ajaxReturn($reData);
    }

}
